package com.cyzy.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cyzy.domain.bean.TAgent;
import com.cyzy.domain.bean.TUserLogin;
import com.cyzy.domain.dto.LoginDto;
import com.cyzy.domain.dto.ResponseData;
import com.cyzy.service.TUserService;
import com.cyzy.utils.RedisUtil;
import com.cyzy.utils.SendSMSUtil;
import com.cyzy.utils.TokenUtil;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import com.cyzy.domain.bean.TUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * TODO
 *
 * @author Honarly
 * @date 2024.11.13
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class TUserController {

    private final TUserService tUserService;

    @Autowired
    private SendSMSUtil sendSMSUtil;

    @Autowired
    private RedisUtil redisUtil;


    @PostMapping("/login")
    public ResponseData login(@RequestBody TUserLogin tUserLogin) {
        String phone= tUserLogin.getPhone();
        String code = String.valueOf(tUserLogin.getCode());
        String codeRedis=redisUtil.get(phone);
        if(code.equals(codeRedis)){
            LambdaQueryWrapper<TUser> qw = new LambdaQueryWrapper<>();
            qw.eq(TUser::getUserPhone, phone);
            // 执行查询
            TUser one = tUserService.getOne(qw);
            System.out.println(one);

            if (one == null) {
                TUser tUser=new TUser();
                tUser.setUserPhone(phone);
                // 未注册--自动注册
                tUserService.save(tUser);
                //注册完成再执行查询
                qw.eq(TUser::getUserPhone, phone);
                one = tUserService.getOne(qw);
                phone=one.getUserPhone();
                // 生成token
                String token= TokenUtil.createToken(phone);
                redisUtil.set(token,phone);
                redisUtil.setExpire(token, 600);
                return ResponseData.success("注册并登录成功", token);

            } else {
                // 已注册
                // 生成token
                String token= TokenUtil.createToken(phone);
                redisUtil.set(token,phone);
                redisUtil.setExpire(token, 600);
                return ResponseData.success("登录成功", token);
            }


        }else {
            return ResponseData.error("验证码错误");
        }


    }

    @PostMapping("/getCode")
    public ResponseData getCode(@RequestBody TUserLogin tUserLogin) {
        String phone= tUserLogin.getPhone();
        System.out.println(phone);
//        SendSMSUtil sendSMSUtil= new SendSMSUtil();
        String result = sendSMSUtil.senSMSUtil(phone);
        int code = sendSMSUtil.getCode();
        redisUtil.set(phone,code);
        redisUtil.setExpire(phone, 300);
        return ResponseData.success(result);
    }

    @PostMapping("/test")
    public ResponseData test(@RequestBody TUserLogin tUserLogin) {
        System.out.println("123123");
        return ResponseData.success("ok");
    }

    @GetMapping("/test2")
    public ResponseData test2() {
        return ResponseData.success("ok");
    }

    // 客服被自动分配的用户信息
    @PostMapping("/findUsers")
    public ResponseData findUsers(@RequestBody List<Integer> ids) {
        List<TUser> users = tUserService.listByIds(ids);
        return ResponseData.success("查询用户信息成功", users, users.size());
    }

    // 获取用户信息
    @PostMapping("/getUserInfo")
    public ResponseData getUserInfo(@RequestBody TUserLogin tUserLogin) {

        System.out.println(tUserLogin);
        String phone = tUserLogin.getPhone();
        LambdaQueryWrapper<TUser> qw = new LambdaQueryWrapper<>();
        qw.eq(TUser::getUserPhone, phone);
        TUser one = tUserService.getOne(qw);
        return ResponseData.success("查询用户信息成功", one);

    }

    //修改用户信息
    @PostMapping("/updateUserInfo")
    public ResponseData updateUserInfo(@RequestBody TUser tUser) {
        System.out.println(tUser.getUserPhone());
        LambdaUpdateWrapper<TUser> uw = new LambdaUpdateWrapper<>();
        // 设置更新条件：用户Id 等于 tUser.getUserId()
        uw.eq(TUser::getUserId, tUser.getUserId());
        // 设置更新内容
        TUser user = new TUser();

        user.setUserPhone(tUser.getUserPhone());
        user.setUserName(tUser.getUserName());
        user.setUserAvatar(tUser.getUserAvatar());

        // 执行更新操作
        boolean result = tUserService.update(user, uw);

        if(result){
            return ResponseData.success("修改用户信息成功");
        }else {
            return ResponseData.error("修改失败");
        }
    }
}
